package cn.mygweb.admin.modules.web.controller;

import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.util.StringUtils;

import cn.mygweb.admin.modules.web.pojo.Message;
import cn.mygweb.common.controller.BaseController;
import cn.mygweb.common.pojo.JsonResult;
import cn.mygweb.common.pojo.RedisKey;
import cn.mygweb.common.utils.IPUtils;
import cn.mygweb.common.utils.JsonUtils;

/** 
* <p>Title: MessageController.java</p>
* <p>Description: 用户留言控制层</p>  
* @author gxb 
* @date 2019年7月7日 
* <p>Copyright: Copyright (c) 2019</p>
* <p>Company: www.mygweb.cn</p> 
*/
@Controller
@RequestMapping("/admin/web/message")
public class MessageController extends BaseController{

	/**
	 * <p>Title: list</p>
	 * <p>Description: 访问到用户留言管理页面</p>
	 * @return
	 */
	@RequestMapping("/list")
	public String list() {
		return "admin/modules/web/message";
	}
	
	/**
	 * <p>Title: ajaxMessageList</p>
	 * <p>Description: 获取用户留言列表</p>
	 * @return
	 */
	@RequestMapping("/ajaxMessageList")
	@ResponseBody
	public JsonResult ajaxMessageList() {
		List<Message> messageList;
		try {
			if(null == super.redisGet(RedisKey.ADMIN_WEB_MESSAGE) || 
					StringUtils.isEmptyOrWhitespace(super.redisGet(RedisKey.ADMIN_WEB_MESSAGE))) {
				messageList = messageService.queryListByExample(new Message());
				super.redisSet(RedisKey.ADMIN_WEB_MESSAGE, JsonUtils.objectToJson(messageList));
			}
			messageList = JsonUtils.jsonToList(super.redisGet(RedisKey.ADMIN_WEB_MESSAGE), Message.class);
		} catch (Exception e) {
			e.printStackTrace();
			messageList = messageService.queryListByExample(new Message());
		}
		return JsonResult.ok(messageList);
	}
	
	/**
	 * <p>Title: sendMessage</p>
	 * <p>Description: 用户发送留言消息</p>
	 * @param message
	 * @return
	 */
	@RequestMapping("/sendMessage")
	@ResponseBody
	public JsonResult sendMessage(Message message) {
		try {
			message.setId(sid.nextShort());
			message.setTime(new Date());
			message.setIp(IPUtils.getIpAddr());
			messageService.save(message);
			super.delRedisCacheByKey(RedisKey.ADMIN_WEB_MESSAGE);
			return JsonResult.ok("发送成功");
		} catch (Exception e) {
			e.printStackTrace();
			return JsonResult.errorMsg("发送失败");
		}
	}
	
	/**
	 * <p>Title: addRemarks</p>
	 * <p>Description: 修改用户留言信息的备注</p>
	 * @param message
	 * @return
	 */
	@RequestMapping("/operation")
	@ResponseBody
	public JsonResult updateRemarks(Message message, String oper) {
		if("add".equals(oper)) {
			return JsonResult.errorMsg("不支持后台添加留言信息");
		}else if("edit".equals(oper)) {
			try {
				if(null != message.getRemarks()) {
					if(null == message.getStatus() || 0 == message.getStatus()) {//将状态设为已读
						message.setStatus(1);
					}
					messageService.update(message);
					super.delRedisCacheByKey(RedisKey.ADMIN_WEB_MESSAGE);
					return JsonResult.ok("操作成功");
				}else {
					return JsonResult.errorMsg("没有添加备注内容");
				}
			} catch (Exception e) {
				e.printStackTrace();
				return JsonResult.errorMsg("操作失败");
			}
		}else if("del".equals(oper)){
			try {
				String[] ids = message.getId().split(",");
				messageService.batchDelByIds(ids);
				super.delRedisCacheByKey(RedisKey.ADMIN_WEB_MESSAGE);
				return JsonResult.ok("删除成功");
			} catch (Exception e) {
				e.printStackTrace();
				return JsonResult.errorMsg("删除失败");
			}
		}
		return null;
	}
	
	/**
	 * <p>Title: ajaxUnreadMsg</p>
	 * <p>Description: 获取未读消息列表，返回给后台管理页面头部的消息提示</p>
	 * @return
	 */
//	@RequestMapping("/getUnreadMsg")
	public String getUnreadMsg(ModelMap model) {
		try {
			List<Message> unreadMsgList = messageService.selectUnreadMsg(-1);
			model.addAttribute("unreadMsgList", unreadMsgList);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "admin/common/header::header";
	}
	
	/**
	 * <p>Title: getUnreadMsg</p>
	 * <p>Description: 获取未读消息列表，返回给后台管理页面头部的消息提示</p>
	 * @return
	 */
	@RequestMapping("/getUnreadMsg")
	@ResponseBody
	public JsonResult getUnreadMsg() {
		try {
			List<Message> unreadMsgList = messageService.selectUnreadMsg(-1);
			return JsonResult.ok(unreadMsgList);
		} catch (Exception e) {
			e.printStackTrace();
			return JsonResult.errorMsg("查询失败");
		}
	}
	
}
